package com.example.wechatrecords;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    private EditText mEdtSearch;
    private ImageView mImgCancel;
    private TextView mTvCancel;
    private LinearLayout mLlSearchView;
    private RelativeLayout mRatContent;
    private TextView mTvSearch;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        //注意要清除 FLAG_TRANSLUCENT_STATUS flag
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        getWindow().setStatusBarColor(getResources().getColor(R.color.transparent));
        setContentView(R.layout.activity_main);

        initView();
        initData();
        initListener();

    }

    private void initData() {
        long timeSeconds = System.currentTimeMillis();
        Log.d(TAG,"timeSeconds---->" +timeSeconds); // 1647082121051
        if (timeSeconds >= 1647514121051L) {
            mRatContent.setVisibility(View.GONE);
        }
    }

    private void initListener() {
        mImgCancel.setOnClickListener(v -> {
            mEdtSearch.setText("");
            mLlSearchView.setVisibility(View.GONE);
        });

        mEdtSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                Log.d(TAG,"beforeTextChanged-->");
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Log.d(TAG,"onTextChanged-->");
            }

            @Override
            public void afterTextChanged(Editable s) {
                String search = mEdtSearch.getText().toString().trim();
                Log.d(TAG,"afterTextChanged-search->" + search);
                mLlSearchView.setVisibility(search.isEmpty() ? View.GONE : View.VISIBLE);
                mImgCancel.setVisibility(search.isEmpty() ? View.GONE : View.VISIBLE);
                mTvSearch.setText(s.toString().trim());
            }
        });

        mTvCancel.setOnClickListener(v -> {
            mEdtSearch.setText("");
            mLlSearchView.setVisibility(View.GONE);
        });

        mLlSearchView.setOnClickListener(v -> {
            startActivity(new Intent(this,MainActivity2.class));
        });
    }

    private void initView() {
        mEdtSearch = findViewById(R.id.edt_search);
        mImgCancel = findViewById(R.id.img_cancel);
        mTvCancel = findViewById(R.id.tv_cancel);
        mLlSearchView = findViewById(R.id.ll_search_view);
        mTvSearch = findViewById(R.id.tv_search);
        mRatContent = findViewById(R.id.rat_content);
    }
}